<template>
  <el-container class="Group-lessons">
    <el-header></el-header>
    <el-main>
      <div class="Group-lessons-main">
        <div class="Group-lessons-main-1">
          <div class="add-title">
            <span>排课信息</span>
            <el-button type="primary" :icon="ArrowLeft" class="text-color-13">
              <router-link to="/mainView/PrivateEducationManagement"
                >返回</router-link
              >
            </el-button>
          </div>
          <el-row class="form-add">
            <el-form
              ref="ruleFormRef"
              :model="ruleForm"
              label-width="120px"
              class="demo-ruleForm"
              :size="formSize"
              status-icon
            >
              <el-form-item label="场馆选择">
                <el-select
                  v-model="value2"
                  class="m-2"
                  placeholder="请选择课程教练"
                >
                  <el-option
                    v-for="item in state.venueList"
                    :key="item.name"
                    :label="item.name"
                    :value="item.id"
                  />
                </el-select>
              </el-form-item>
              <el-form-item label="教练：">
                <div>
                  <el-checkbox-group v-model="checkedCities" :min="0" :max="1">
                    <el-checkbox
                      v-for="item in coach.coachList"
                      :key="item.id"
                      :label="item.id"
                      >{{ item.name }}</el-checkbox
                    >
                  </el-checkbox-group>
                </div>
              </el-form-item>
              <el-form-item label="上课日期：">
                <el-date-picker
                  v-model="value1"
                  type="daterange"
                  range-separator="To"
                  start-placeholder="Start date"
                  end-placeholder="End date"
                />
              </el-form-item>
              <el-form-item label="课程时间：">
                <el-time-picker
                  is-range
                  v-model="time1"
                  range-separator="至"
                  start-placeholder="开始时间"
                  end-placeholder="结束时间"
                  placeholder="选择时间范围"
                >
                </el-time-picker>
              </el-form-item>
              <el-form-item label="单节人数上限：">
                <el-input
                  v-model="ruleForm.privateCourseInfoAddDto.personMaxinum"
                  suffix-icon="currency"
                />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="submitForm(ruleFormRef)"
                  >保存</el-button
                >
                <el-button @click="resetForm(ruleFormRef)">重置</el-button>
              </el-form-item>
            </el-form>
          </el-row>
        </div>
        <el-row class="table-margin">
          <div class="Group-lessons-main-1">
            <el-table
              :data="tableData"
              border
              style="width: 100%"
              :header-cell-style="{ background: '#f9fafc', color: '#606266' }"
            >
              <el-table-column type="selection" width="55" />
              <el-table-column prop="date" label="会员卡名称" width="180" />
              <el-table-column prop="name" label="会员卡类型" width="180" />
              <el-table-column prop="address" label="支持场馆" width="180" />
              <el-table-column prop="addre" label="扣费设置" width="180">
                <template #defualt>
                  <p>fwegwgwg</p>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-row>
      </div>
    </el-main>
  </el-container>
</template>

<script lang="ts" setup>
import { ElMessage } from "element-plus";
import { ArrowLeft } from "@element-plus/icons-vue";
import { reactive, ref, nextTick, onMounted } from "vue";
import type { FormInstance, FormRules } from "element-plus";
import { ElInput } from "element-plus";
import { addPrivateCourseSchedule } from "../../http/api";
import { on } from "events";
import { findVenue, findCoach, addCourseSchedule } from "../../http/api";
import { useRoute } from "vue-router";
import router from "../../router";
let zid = useRoute().query.zid;

const formSize = ref("default");
const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive({
  coachId: "",
  courseEndDate: "",
  courseStartDate: "",
  privateCourseId: "",
  privateCourseInfoAddDto: {
    endTime: 0,
    personMaxinum: 0,
    startTime: 0,
  },
  venueId: "",
});
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      var a = getTime(value1.value[0]);
      var b = getTime(value1.value[1]);
      var c = getHours(time1.value[0]);
      var d = getHours(time1.value[1]);
      ruleForm.coachId = checkedCities.value[0];
      ruleForm.courseStartDate = a;
      ruleForm.courseEndDate = b;
      ruleForm.privateCourseInfoAddDto.startTime = c;
      ruleForm.privateCourseInfoAddDto.endTime = d;
      ruleForm.venueId = value2.value;
      ruleForm.privateCourseId = zid;
      console.log(ruleForm);
      addCourseSchedule(ruleForm).then((res) => {
        if (res.code == 0) {
          router.push({
            path: "/mainView/PrivateEducationManagement",
          });
        }
      });
    } else {
      console.log("error submit!", fields);
    }
  });
};

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};

const options = Array.from({ length: 10000 }).map((_, idx) => ({
  value: `${idx + 1}`,
  label: `${idx + 1}`,
}));
// 教练
const value3 = ref("");
// 场馆
const value2 = ref("");
// 日期选择
const value1 = ref("");
// 时间选择
const time1 = ref([new Date(2016, 9, 10, 8, 40), new Date(2016, 9, 10, 9, 40)]);

// 标签
const inputValue = ref("");
const dynamicTags = ref(["Tag 1", "Tag 2", "Tag 3"]);
const inputVisible = ref(false);
const InputRef = ref<InstanceType<typeof ElInput>>();

const handleClose = (tag: string) => {
  dynamicTags.value.splice(dynamicTags.value.indexOf(tag), 1);
};

const showInput = () => {
  inputVisible.value = true;
  nextTick(() => {
    InputRef.value!.input!.focus();
  });
};

const handleInputConfirm = () => {
  if (inputValue.value) {
    dynamicTags.value.push(inputValue.value);
  }
  inputVisible.value = false;
  inputValue.value = "";
};
// 会员卡表格
const tableData = [
  {
    date: "体验卡片",
    name: "储值类型",
    address: "健身馆",
  },
];
// 时间转换函数
const getTime = (a) => {
  var d = new Date(a);
  var datetime = d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate();
  return datetime;
};
// 时间转换函数2
const getHours = (a) => {
  var d = new Date(a);
  var datetime = d.getHours();
  return datetime;
};
// 教练列表
const checkedCities = ref();
const cities = ["Shanghai", "Beijing", "Guangzhou", "Shenzhen"];
// 场馆列表
var state = reactive({ venueList: [] });
var coach = reactive({ coachList: [] });
onMounted(() => {
  findVenue({ limit: 20, page: 1 }).then((res) => {
    if (res.code == 0) {
      state.venueList = res.data;
    }
  });
});
// 教练
findCoach().then((res) => {
  console.log(res);
  if (res.code == 0) {
    coach.coachList = res.data;
  }
});
</script>

<style scoped>
.Group-lessons {
  background-color: #f2f2f2;
  height: 100vh;
}

.Group-lessons-main {
  background-color: #ffffff;
  border-radius: 10px;
  padding: 10px;
}

.Group-lessons-main-1 {
  border-radius: 15px;
  border: 1px solid #e4e4e4;
  overflow: hidden;
}
.table-margin {
  padding: 20px 0;
  display: flex;
  justify-content: center;
}

.add-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #f3f3f3;
  padding: 5px 10px;
}

.form-add {
  padding: 20px 100px;
}

.currency::before {
  content: "元";
}

.el-popper.is-customized {
  /* Set padding to ensure the height is 32px */
  padding: 6px 12px;
  background: linear-gradient(90deg, rgb(159, 229, 151), rgb(204, 229, 129));
}

.el-popper.is-customized .el-popper__arrow::before {
  background: linear-gradient(45deg, #b2e68d, #bce689);
  right: 0;
}

.el-tag {
  margin-left: 10px !important;
}

.button-new-tag {
  margin-left: 10px !important;
}
/* 多选框 */
.checked-coach {
  display: block;
  margin: 0;
}
.text-color-13 a {
  text-decoration: none;
  color: white;
}
</style>
